<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\Qh_LitQuestionRequest;
use App\Models\Qh_LitQuestion;
use App\Models\Qh_Question;
use Illuminate\Http\JsonResponse;

class Qh_LitQuestionController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = Qh_LitQuestion::filter(\request()->all())
            ->with(['parent' => function($q) {
                $q->select(['id', 'title']);
            }])
            ->orderByDesc('id')
            ->paginate(\request('perPage', 10));
        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Qh_LitQuestionRequest $request
     * @return JsonResponse|object
     */
    public function store(Qh_LitQuestionRequest $request)
    {
        $params = $request->validated();
        $parent = Qh_Question::findOrFail($params['parentId']);
        if (empty($params['qlId'])) $params['qlId'] = $parent->qlId;
        if (empty($params['courseId'])) $params['courseId'] = $parent->courseId;
        if (empty($params['optionCount'])) $params['optionCount'] = count(explode('|NTK|', $params['options']));
        if (empty($params['difficulty'])) $params['difficulty'] = 0;
        if (empty($params['difficultyValue'])) $params['difficultyValue'] = 0;
        if (empty($params['rightCount'])) $params['rightCount'] = 0;
        if (empty($params['wrongCount'])) $params['wrongCount'] = 0;

        $notice = Qh_LitQuestion::create($params);

        return $this->restful($notice);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function show(int $id)
    {
        return $this->restful();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Qh_LitQuestionRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(Qh_LitQuestionRequest $request, int $id)
    {
        $params = $request->validated();
        $parent = Qh_Question::findOrFail($params['parentId']);
        if (empty($params['qlId'])) $params['qlId'] = $parent->qlId;
        if (empty($params['courseId'])) $params['courseId'] = $parent->courseId;
        $params['optionCount'] = count(explode('|NTK|', $params['options']));

        $result = Qh_LitQuestion::where('id', $id)->update($params);

        return $this->restful($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function destroy(int $id)
    {
        $result = Qh_LitQuestion::where('id', $id)->delete();
        return $this->restful($result);
    }

    public function questionDeleteByIds() {
        $ids = explode(',', \request('id'));

        $knowledgeIds = [];
        $questions = Qh_Question::findMany($ids);

        foreach ($questions as $question) {
            $knowledgeIds[] = $question->knowledgeId;
        }

        $result = Qh_Question::whereIn('id', $ids)->delete();

        // 试题数量更新
        foreach ($knowledgeIds as $knowledgeId) {
            updateQhQuestionCount($knowledgeId, 1, 2);
        }
        return $this->restful($result);
    }
}
